<template>
  <div class="main_spac1 maincl">
    <div class="main_white padding10 paddingBottom20">
      <div class="mainor_form">
        <div class="mainor_te">
          <el-button
            type="primary"
            @click="jumpOrderAdd"
            :loading="jumpLoading"
            v-repeatClick
            >我要批发</el-button
          >
          <el-button @click="exportbuVisible = true">导出</el-button>
        </div>
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="70px"
        >
          <el-form-item label="批发单号" prop="order_sn">
            <el-input
              v-model.trim="form.order_sn"
              placeholder="批发单号"
              clearable
              class="wid220"
            ></el-input>
          </el-form-item>
          <el-form-item label="配送日期" prop="sendTime">
            <el-date-picker
              v-model="sendTime"
              type="daterange"
              class="time220"
              :editable="false"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :clearable="false"
              :picker-options="pickerOptions"
            />
          </el-form-item>

          <el-form-item label="状态" prop="order_status">
            <el-select
              v-model.trim="form.order_status"
              placeholder="状态"
              class="wid220"
            >
              <el-option label="所有" :value="parseInt(0)"></el-option>
              <el-option
                v-for="(group, index) in order_status_list"
                :key="index"
                :label="group"
                :value="parseInt(index) + 1"
              ></el-option>
            </el-select>
          </el-form-item>
          <div class="more el-form-item" @click="more">
            <span>更多</span>
            <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
          </div>
          <div class="clearfix"></div>
          <el-collapse-transition>
            <div v-if="moredata">
              <div class="demo">
                <!-- <el-form-item label="支付状态" prop="pay_status">
                  <el-select
                    v-model.trim="form.pay_status"
                    placeholder="状态"
                    class="wid220"
                  >
                    <el-option label="所有" :value="parseInt(0)"></el-option>
                    <el-option
                      v-for="(group, index) in pay_status_list"
                      :key="index"
                      :label="group"
                      :value="parseInt(index) + 1"
                    ></el-option>
                  </el-select>
                </el-form-item> -->
                <el-form-item
                  label="管理员"
                  prop="admin"
                  style="margin-right: 0"
                >
                  <el-select
                    v-model.trim="form.admin"
                    placeholder="管理员"
                    class="wid220"
                  >
                    <el-option label="所有" value="0"></el-option>
                    <el-option
                      v-for="(group, index) in admin_users"
                      :key="index"
                      :label="group"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="批发商" prop="wholesaler">
                  <el-select
                    v-model.trim="form.wholesaler"
                    placeholder="分组"
                    class="wid220"
                  >
                    <el-option label="所有" value="0"></el-option>
                    <el-option
                      v-for="(group, index) in companies"
                      :key="index"
                      :label="group"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="创建日期">
                  <el-date-picker
                    v-model="createTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="pickerOptions"
                  />
                </el-form-item>
              </div>
            </div>
          </el-collapse-transition>

          <el-form-item class="marginTop10">
            <el-button class="reset marginLeft10" type="primary" @click="reset"
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <div class="main_table" style="min-height: 570px">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :header-cell-class-name="this.$verticaline"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" />
          <el-table-column
            label="批发单号"
            prop="order_sn"
            width="200"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="批发商" prop="" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{ scope.row.dispatch ? scope.row.dispatch.company_name : "" }}
            </template>
          </el-table-column>
          <el-table-column
            label="下单金额"
            prop="order_money"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="配送金额"
            prop="send_money"
            :show-overflow-tooltip="true"
          >
          </el-table-column>

          <el-table-column
            label="配送日期"
            prop=""
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{
                scope.row.send_time
                  ? parseTime(scope.row.send_time, "{y}-{m}-{d}")
                  : ""
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="创建日期"
            prop=""
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{
                scope.row.created_at
                  ? parseTime(scope.row.created_at, "{y}-{m}-{d}")
                  : ""
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="订单状态"
            prop="in_pass_num"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span :class="'order' + scope.row.order_status">
                {{ order_status_list[scope.row.order_status] }}
              </span>
            </template>
          </el-table-column>
          <!-- <el-table-column
            label="支付状态"
            prop="in_pass_num"
            :show-overflow-tooltip="true"
            ><template slot-scope="scope">
              <span :class="scope.row.pay_status == 0 ? 'unpaid' : 'success'">
                {{ pay_status_list[scope.row.pay_status] }}
              </span>
            </template>
          </el-table-column> -->
          <el-table-column
            label="管理操作"
            align="left"
            fixed="right"
            width="240"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <router-link :to="'/bulk/order/see/' + scope.row.id">
                <el-link type="success">查看</el-link>
              </router-link>
              <el-link
                type="warning"
                v-if="scope.row.order_status !== 4 && scope.row.pay_status == 0"
                @click="handleCancel(scope)"
                >取消</el-link
              >
              <el-link
                type="primary"
                v-if="scope.row.order_status !== 4 && scope.row.pay_status == 0"
                @click="handlePay(scope)"
                >支付</el-link
              >
              <router-link :to="'/bulk/order/record/' + scope.row.id">
                <el-link type="primary" v-if="scope.row.order_status == 2"
                  >收货</el-link
                ></router-link
              >

              <el-link @click="exportDetail(scope.row.id)">导出</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>
    <!-- 订单导出 -->
    <el-dialog
      title="订单导出"
      :visible.sync="exportbuVisible"
      class="main_dialog dialog500"
    >
      <div v-if="selectArr.length == 0 ? true : false">
        <p class="exptitle">配送日期</p>
        <el-date-picker
          v-model="exportTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          @change="datepicker"
        >
        </el-date-picker>
        <!-- <el-tooltip
          class="item"
          effect="dark"
          content="时间范围只能选择一个月之内"
          placement="top"
        >
          <i class="el-icon-question"></i>
        </el-tooltip> -->
      </div>
      <div class="selectTitle" v-if="selectArr.length == 0 ? false : true">
        已选择{{ selectArr.length }}条订单
      </div>
      <div>
        <div style="margin-top: 15px">
          <el-checkbox v-model="goods_head">导出商品信息 </el-checkbox>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="exportbuVisible = false">取 消</el-button>
        <el-button type="primary" @click="exportConfirm" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  bulk_order,
  bulk_order_cancel,
  bulk_order_finish,
  bulk_order_export,
  bulk_order_info_export,
  bulk_order_checkBind,
} from "@/api/bulk/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime, exportMethod } from "@/utils/index";
import dateOption from "@/options/dateOption";

export default {
  data() {
    return {
      parseTime,
      exportMethod,
      createTime: [],
      sendTime: [],
      pickerOptions: dateOption,
      // 搜索
      form: {
        order_sn: "", //批发单号
        send_time_start: "", //配送日期
        send_time_end: "",
        order_status: "", //订单状态
        pay_status: "", //支付状态
        admin: "", //管理员
        wholesaler: "",
        create_time_start: "", //创建日期
        create_time_end: "",
      },

      // 更多
      moredata: false,
      // 列表数据
      list: [],
      order_status_list: [],
      pay_status_list: [],
      companies: [],
      admin_users: [],
      listLoading: false,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      selectArr: [],
      // 导出
      // 默认的导出表头信息
      goods_head: false, //商品信息 true
      exportbuVisible: false,
      // 订单导出时间
      exportTime: [],
      jumpLoading: false,
    };
  },
  methods: {
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push(item.id);
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.bulk_order();
    },
    changePage(page) {
      this.current.page = page;
      this.bulk_order();
    },
    // 导出时间范围
    datepicker(val) {
      let start = "";
      let end = "";
      if (val) {
        start = new Date(val[0]).getTime();
        end = new Date(val[1]).getTime();
        if (end - start > 30 * 24 * 60 * 60 * 1000) {
          this.$message.warning("选择范围不能超过一个月，请重新选择");
          this.exportTime = "";
        }
      }
    },
    exportConfirm() {
      if (this.selectArr.length == 0 && this.exportTime.length == 0) {
        this.$message.warning("请先选择需要导出的数据");
        return;
      }
      let form = {
        ids: "",
        goods_head: this.goods_head ? 1 : 0,
        start_send_time: "",
        end_send_time: "",
      };
      if (this.selectArr.length > 0) {
        form.ids = this.selectArr.join(",");
      } else {
        form.start_send_time = this.exportTime[0];
        form.end_send_time = this.exportTime[1];
      }

      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      bulk_order_export(data).then((res) => {
        this.exportMethod(res, "批发订单");
      });
      this.exportbuVisible = false;
    },
    // 详情导出
    exportDetail(id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      bulk_order_info_export(data).then((res) => {
        this.exportMethod(res, "批发订单详情");
      });
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.bulk_order();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.sendTime = [];
      this.createTime = [];
      this.bulk_order();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    bulk_order() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      bulk_order(data).then((res) => {
        if (res.errcode == 0) {
          this.list = res.data.list.data;
          this.order_status_list = res.data.order_status_list;
          this.pay_status_list = res.data.pay_status_list;
          this.admin_users = res.data.admin_users;
          this.companies = res.data.companies;
          this.totalCount = res.data.list.total;
          this.listLoading = false;
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.send_time_start = this.sendTime ? this.sendTime[0] : "";
      form.send_time_end = this.sendTime ? this.sendTime[1] : "";
      form.create_time_start = this.createTime ? this.createTime[0] : "";
      form.create_time_end = this.createTime ? this.createTime[1] : "";
      return form;
    },
    // 取消
    handleCancel(scope) {
      let id = scope.row.id;
      this.$confirm("确定取消订单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let data = Object.assign(
          {
            sign: Md5(
              objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
            ),
          },
          apiUrl().name,
          { id: id }
        );
        bulk_order_cancel(data).then((res) => {
          if (res.errcode == 0) {
            this.bulk_order();
          } else {
            this.$message.error(res.errmsg);
          }
        });
      });
    },
    // 支付
    handlePay(scope) {
      this.$router.push({
        name: "bulk_shopcart_pay",
        params: { oid: scope.row.id },
      });
    },
    // 我要批发
    jumpOrderAdd() {
      this.jumpLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      bulk_order_checkBind(data).then((res) => {
        if (res.errcode == 0) {
          this.jumpLoading = false;
          this.$router.push("/bulk/order/add");
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.bulk_order();
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.mainor_form form {
  width: calc(100% - 280px);
}

// 支付状态
.success {
  color: #03a98e;
}
.unpaid {
  color: #f56c6c;
}
// 订单状态
.order0 {
  color: #f56c6c;
}
.order1 {
  color: #118ab2;
}
.order2 {
  color: #aacd6e;
}
.order3 {
  color: #03a98e;
}
.order4 {
  color: #cccccc;
}
// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
  cursor: pointer;
}
.selectTitle {
  font-size: 20px;
  text-align: center;
}
.margintop {
  padding-top: 15px;
}
.el-checkbox-group {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  .el-checkbox {
    width: 26%;
  }
}
</style>
